package machine.received.service.impl;

import machine.bean.Msg;
import machine.bean.MsgType;
import machine.received.service.ReceivedMsgService;
import machine.send.service.SendService;
import machine.utils.MsgDequeUtils;
import machine.utils.MsgUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * @author zhanghui145
 * @date 2021/12/25 9:36
 * @since 1.0.0
 */
@Service
public class ReceivedMsgServiceImpl implements ReceivedMsgService {

    @Autowired
    private SendService sendService;

    @Override
    public void receivedMsg(HttpServletRequest request){
        Msg msg = MsgUtils.getMsgByRequest(request);
        if (msg == null) {
            return;
        }
        this.receivedSendMsg(msg);
    }

    @Override
    public void receivedSendMsg(Msg msg){
        Integer msgId = msg.getMessage_id();
        if (MsgDequeUtils.check(msgId)) {
            return;
        }
        MsgDequeUtils.put(msgId);
        String messageType = msg.getMessage_type();
        Integer id = null;
        if (MsgType.GROUP.getType().equals(messageType)) {
            id = msg.getGroup_id();
        }else if(MsgType.PRIVATE.getType().equals(messageType)) {
            id = msg.getSender().getUser_id();
        }
        sendService.reply(msg, messageType, id, msg.getRaw_message());
    }
}
